package mystreamutils;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * 用于客户端、服务端之间相互传输数据的工具类
 */
public abstract class StreamUtils {
    //服务端相关操作
    //服务端发送信息
    public static void serverSendingInformation(Socket accept, String information) throws IOException {
        if (accept == null) {
            throw new NullPointerException("serverSocket == null");
        }
        if (information == null) {
            throw new NullPointerException("information == null");
        }

        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        System.out.println("正在向主机 " + accept.getInetAddress() + " 发送消息...");
        bufferedWriter.write(information);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        accept.shutdownOutput();

        bufferedWriter.close();
        accept.close();
        //serverSocket.close();
        System.out.println("消息发送完毕!");
    }

    //服务端接收信息
    public static String serverReceivingInformation(Socket accept) throws IOException {
        if (accept == null) {
            throw new NullPointerException("serverSocket == null");
        }

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        System.out.println("正在接收主机 " + accept.getInetAddress() + " 发送的消息...");
        String information = bufferedReader.readLine();

        bufferedReader.close();
        accept.close();
        //serverSocket.close();
        System.out.println("消息接收完毕!");

        return information;
    }

    //客户端相关操作
    //客户端发送信息
    public static void sendingInformation(Socket clientSocket, String information) throws IOException {
        if (clientSocket == null) {
            throw new NullPointerException("clientSocket == null");
        }
        if (information == null) {
            throw new NullPointerException("information == null");
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
        System.out.println("正在向服务端发送消息...");
        bufferedWriter.write(information);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        clientSocket.shutdownOutput();

        bufferedWriter.close();
        //clientSocket.close();
        System.out.println("已成功将消息发送到服务端!");
    }

    //客户端接收信息
    public static String receivingInformation(Socket clientSocket) throws IOException {
        if (clientSocket == null) {
            throw new NullPointerException("clientSocket == null");
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String information = bufferedReader.readLine();

        bufferedReader.close();
        return information;
    }
}

class test {
    public static void main(String[] args) throws IOException {
        int port = 9999;
        ServerSocket serverSocket = new ServerSocket(port);
        //Socket clientSocket = new Socket(serverSocket.getInetAddress(), port);
        Socket clientSocket = new Socket(InetAddress.getLocalHost(), port);

        System.out.println("等待主机连接...");

        Socket accept = serverSocket.accept();
        System.out.println("已有主机连接 " + accept.getPort() + " 端口!");

        StreamUtils.sendingInformation(clientSocket, "你好服务端！");
        System.out.println(StreamUtils.serverReceivingInformation(accept));
        StreamUtils.serverSendingInformation(accept, "你好客户端！");
        System.out.println(StreamUtils.receivingInformation(clientSocket));

        clientSocket.close();
        serverSocket.close();
    }
}
